let initialState = {
    token: '',
    questions:[],
    question_id:'',
    test_id:'',
    keyPath:'',
    goodid: '',
}

export default function reducer(state = initialState, action: any) {
    // 1. 深拷贝
    let newstate = JSON.parse(JSON.stringify(state))

    // 2.判断action类型
    // token
    if (action.type == 'SAVE_TOKEN') {
        newstate.token = action.payload
    } else if (action.type == 'REMOVE_TOKEN') {
        newstate.token = ''
    } else if (action.type == 'SAVE_QUESTIONS') {
        newstate.questions = action.payload
    } else if (action.type == 'SAVE_QUESTIONS_ID') {
        newstate.question_id = action.payload
    } else if (action.type == 'SAVE_TEST_ID') {
        newstate.test_id = action.payload
    } else if (action.type == 'SAVE_KEYPATH') {
        newstate.keyPath = action.payload.reverse()
    } else if (action.type == 'SAVE_ID') {
        newstate.id = action.payload;
    } else if (action.type == 'REMOVE_ID') {
        newstate.id = ''
    } else if (action.type == 'SAVE_GOODID') {
        newstate.goodid = action.payload

    }else {
        return state

    }

    // 3.返回全新的state
    return newstate
}